<?php
/*
 * get valid session string
 */
require_once ('../lib/config.php');
require_once ('../lib/facebook.php');
require_once ('../lib/util.php');

//session_start();
trace($_COOKIE);
$facebook = new Facebook(array(
    'appId'  => FB_APP_ID,
    'secret' => FB_APP_SECRET,
    'cookie' => true
));


//trace($session);
//Buttongs
$logout_button = "<a href='".$facebook->getLogoutUrl()."'><img src='http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif'></a>"; 
$login_button = "<a href='".$facebook->getLoginUrl()."'><img src='http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif'></a>";

$me = null;
if(isLoggedIn()){
    try{
        $me = $facebook->api('/me');
trace($me);        
        header('Location: ./TestAuth_page2.php?session='.http_build_query($session));
    }catch(FacebookApiException $e){
        error_log($e);
//        header('Location: http://m.facebook.com/logout.php?confirm=1&next=http://localhost/o-my-friends/test/testauth_page1.php');
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TEST</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="../javascript/util.js"></script>
<script type="text/javascript">

$(document).ready(function(){

	$('#loading')
		.css('position', 'absolute')
		.css('left', $(document).width()/2)
		.css('top', $(document).height()/2)
		.hide();

	$(window).resize(function(){
		$('#loading')
			.css('position', 'absolute')
    		.css('left', $(document).width()/2)
    		.css('top', $(document).height()/2);
	});

<?php if($me){?>
//	getFriendsAJAX();
<?php }?>
});

function getFriendsAJAX(){
	$('#loading').show();
	$.getJSON('/o-my-friends/lib/getFriendsAJAX.php', null, function(data){
		$('#loading').hide();
		//Build Friend Table
		buildFriendTable(data);
	});
}

function buildFriendTable(data){
	var tbody = $('#friendsTable tbody');
	$(data).each(function(item, value){
		tbody.append("<tr><td>"+ item +"</td><td>"+ value.name +"</td></tr>");
	});
}

</script>
</head>
<body>
<div id="loading"><img src="/o-my-friends/images/ajax-loader.gif"/></div>
<?php print(($me)? $logout_button : $login_button);?>
<div id="friendsContainer">
	<table id="friendsTable">
		<thead>
			<tr>
				<th>no</th>
				<th>name</th>
			</tr>
		</thead>
		<tbody></tbody>
	</table>
</div>
</body>
</html>
